//
//  DetailTVController.m
//  CarStore
//
//  Created by 千锋 on 16/1/27.
//  Copyright (c) 2016年 1000phone. All rights reserved.
//

#import "DetailTVController.h"
/** 三个cell*/
#import "DetailMainCell.h"
#import "DetailManagerCell.h"
#import "DetailSelectedCell.h"

#import "ManagerModel.h"
#define API_MANAGERS @"http://api.cacaqc.com/keeper/KeeperList"

@interface DetailTVController ()

@property (nonatomic, strong) NSMutableArray *managers;

@end

@implementation DetailTVController

- (NSMutableArray *)managers
{
    if (!_managers) {
        _managers = [[NSMutableArray alloc]init];
    }
    return _managers;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self createTableView];
    [self requestData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - ui相关
/** 界面相关*/
- (void)createTableView
{
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailMainCell" bundle:nil] forCellReuseIdentifier:@"DetailMainCellID"];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailManagerCell" bundle:nil] forCellReuseIdentifier:@"DetailManagerCellID"];
    [self.tableView registerNib:[UINib nibWithNibName:@"DetailSelectedCell" bundle:nil] forCellReuseIdentifier:@"DetailSelectedCellID"];
    
    self.tableView.estimatedRowHeight = 400;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.allowsSelection = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.bounces = NO;
}

#pragma mark - 数据相关
/** 创建下拉刷新控件*/
- (void)requestData
{
    [MBProgressHUD showMessage:@"正在加载..."];
    [AFHTTPSessionManager POST:API_MANAGERS parmaeters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *body = responseObject[@"body"];
        NSMutableArray *dataLists = body[@"dataList"];
        
            for (NSDictionary *dic in dataLists) {
                    ManagerModel *model = [ManagerModel modelWithDict:dic];
                    [self.managers addObject:model];
                }
        [self showSuccess:YES status:@"加载成功"];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        [self showSuccess:NO status:@"加载失败"];
    }];
}
// 停止隐藏并提示是否加载成功
- (void)showSuccess:(BOOL)isSucc status:(NSString *)status
{
    [MBProgressHUD hideHUD];
    if (isSucc) {
        [SVProgressHUD showSuccessWithStatus:status];
        [self.tableView reloadData];
    }else{
        [SVProgressHUD showErrorWithStatus:status];
    }
}

#pragma mark - TablViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.managers.count + 2;
}
/** 创建cell*/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        DetailMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailMainCellID" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.model;
        return cell;
    }else if(indexPath.row == 1){
        DetailSelectedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailSelectedCellID" forIndexPath:indexPath];
        return cell;
    }else{
        DetailManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailManagerCellID" forIndexPath:indexPath];
        ManagerModel *model = self.managers[indexPath.row - 2];
        
        cell.model = model;
        [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.head_img] placeholderImage:[UIImage imageNamed:@"placeholder_image"]];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PRINT;
}



@end
